package com.jn.news.ui;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.jn.news.R;
import com.jn.news.adapter.TianxingNewsAdapter;
import com.jn.news.model.NewsDto;
import com.jn.news.utils.Constant;
import com.jn.news.utils.DatabaseHelper;
import com.jn.news.utils.SPUtils;
import com.jn.news.utils.StatusBarUtils;
import com.jn.news.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的收藏
 */
public class MyCollectionActivity extends AppCompatActivity {

    private RelativeLayout rlLeft;
    private RecyclerView mRv;
    private List<NewsDto> mList = new ArrayList<>();
    private TianxingNewsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.with(this).init3(2, 102);
        setContentView(R.layout.activity_my_collection);
        initView();
        initData();

    }

    private void initView() {
        rlLeft = findViewById(R.id.rl_left);
        mRv = findViewById(R.id.mRv);
    }
    private void initData(){

        rlLeft.setOnClickListener(v -> finish());
        mRv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new TianxingNewsAdapter(this);
        mRv.setAdapter(adapter);


    }

    @Override
    protected void onResume() {
        super.onResume();
        getCollectionList();
    }

    /**
     * 获取订单列表
     */
    private void getCollectionList() {
        mList.clear();
        //初始化数据库
        SQLiteOpenHelper dbHelper = new DatabaseHelper(this);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        // 查询所有数据
        Cursor cursor = db.query("collection", null, "user_id = ?", new String[] {(int) SPUtils.get(this, Constant.USERID,0)+""}, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                //这里是查询的ticketorder表中的所有数据
                int id = cursor.getInt(cursor.getColumnIndexOrThrow("id"));
                String userId = cursor.getString(cursor.getColumnIndexOrThrow("user_id"));
                String newsId = cursor.getString(cursor.getColumnIndexOrThrow("news_id"));
                String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
                String ctime = cursor.getString(cursor.getColumnIndexOrThrow("ctime"));
                String source = cursor.getString(cursor.getColumnIndexOrThrow("source"));
                String picurl = cursor.getString(cursor.getColumnIndexOrThrow("picurl"));
                String url = cursor.getString(cursor.getColumnIndexOrThrow("url"));
                mList.add(new NewsDto(newsId,ctime,title,source,picurl,url));
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        if (mList.size() == 0) {
            ToastUtil.showToast(this, "暂无收藏");
        }
        adapter.refresh(mList);
    }

}